﻿package ua.mihaylenko.canvas.gui.dscene
{
	
	import flash.display.BitmapData;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.ui.Mouse;
	import org.papervision3d.lights.PointLight3D;
	import org.papervision3d.materials.utils.MaterialsList;
	import org.papervision3d.view.BasicView;
	import ua.mihaylenko.canvas.gui.photoshop.canvas.GUICanvas;
	
	import org.papervision3d.events.InteractiveScene3DEvent;
	
	import org.papervision3d.scenes.Scene3D;
	import org.papervision3d.cameras.*;
	import org.papervision3d.materials.*;
	import org.papervision3d.scenes.Scene3D;
	import org.papervision3d.events.RendererEvent;
	import org.papervision3d.objects.primitives.Cube;
	import ua.mihaylenko.canvas.gui.GUISimpleWindow;
	import flash.events.Event;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.BlendMode;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import ua.mihaylenko.canvas.controller.EventManager;
	import ua.mihaylenko.canvas.controller.CanvasEvent;
	import ua.mihaylenko.canvas.data.DataManager;
	public class GUI3ds extends  GUISimpleWindow{
		public var _smcBackground:Sprite;
		public var _btn2DView:SimpleButton;
		private var view:BasicView;
		private var _cCube:Cube;
		private var _pStart:Point;
		private var light:PointLight3D;
	
		public static var IS_3D:Boolean = false;
		
		public function GUI3ds() {
			EventManager.getInstance().addEventListener(CanvasEvent.VIEW_3D, init);
			EventManager.getInstance().addEventListener(CanvasEvent.VIEW_2D, release);
			this.visible = false;
			_btn2DView.useHandCursor = false;
			_btn2DView.addEventListener(MouseEvent.CLICK, clickOnShow2D);
			light = new PointLight3D(true);
			light.x = 200;
			light.y = 200;

		}
		private function clickOnShow2D(event:MouseEvent) {
			EventManager.getInstance().dispatchEvent(new CanvasEvent(CanvasEvent.VIEW_2D));
		}
		public function init(event:CanvasEvent) {
			this.visible = true;
			view = new BasicView(_smcBackground.width / 2, _smcBackground.height / 2);
			view.x = 100+_smcBackground.x- _smcBackground.width / 2.0;
			view.y = 100+_smcBackground.y - _smcBackground.height / 1.5;
			this.addChild(view);
			this.addChild(_smcBackground);
			_smcBackground.alpha = 0;
			_smcBackground.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDownHandler);
			init3D();
			createCube();
			view.startRendering();
			view.scene.addChild(light);

		}
		public function release(event:CanvasEvent) {
			if(view){
				view.stopRendering();
				_smcBackground.removeEventListener(MouseEvent.MOUSE_DOWN, onMouseDownHandler);
				view.scene.removeChild(_cCube);
				this.removeChild(view);
				this.visible = false;
				view = null;
			}
			IS_3D = false;
		}
		private function createCube() {
			var cMaterialsList:MaterialsList = new MaterialsList();
			
			var cBitmapMaterial:BitmapMaterial;
			cBitmapMaterial = new BitmapMaterial(new BackImage(500, 500));
			cMaterialsList.addMaterial(cBitmapMaterial, "front");
			
			cBitmapMaterial= new BitmapMaterial(GUICanvas.THIS.getImageData("front"));
			cMaterialsList.addMaterial(cBitmapMaterial, "back");
			
			cBitmapMaterial= new BitmapMaterial(GUICanvas.THIS.getImageData("top"));
			cMaterialsList.addMaterial(cBitmapMaterial, "top");
			cBitmapMaterial= new BitmapMaterial(GUICanvas.THIS.getImageData("bottom"));
			cMaterialsList.addMaterial(cBitmapMaterial, "bottom");
			cBitmapMaterial= new BitmapMaterial(GUICanvas.THIS.getImageData("left"));
			cMaterialsList.addMaterial(cBitmapMaterial, "left");
			cBitmapMaterial= new BitmapMaterial(GUICanvas.THIS.getImageData("right"));
			cMaterialsList.addMaterial(cBitmapMaterial, "right");

			cBitmapMaterial.smooth = true;
			
			_cCube = new Cube(cMaterialsList, GUICanvas.W, GUICanvas.D, GUICanvas.H, 3, 3, 3);

			_cCube.rotationY = 10;
			_cCube.rotationX = 10
			view.scene.addChild(_cCube, "cube");
			IS_3D = true;
		}
		private function onMouseDownHandler(event:MouseEvent) {
			var tmpSprite:Sprite = new Sprite();
			tmpSprite.graphics.beginFill(0x92EDFA, 0);
			tmpSprite.graphics.drawRect( -10, -10, 20, 20);
			tmpSprite.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMoveHandler);
			tmpSprite.addEventListener(MouseEvent.MOUSE_UP, onMouseUpHandler);
			this.addChild(tmpSprite);
			_pStart = new Point(Number(this.mouseX - _cCube.rotationY), Number(this.mouseY - _cCube.rotationX) )
			tmpSprite.startDrag(true);
		}
		private function onMouseUpHandler(event:MouseEvent) {
			var tmpSprite:Sprite = Sprite(event.target);
			tmpSprite.removeEventListener(MouseEvent.MOUSE_UP, onMouseUpHandler);
			tmpSprite.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMoveHandler);
			this.removeChild(tmpSprite);
			//tmpSprite = null;
		}
		private function onMouseMoveHandler(event:MouseEvent) {
			_cCube.rotationY = _pStart.x - this.mouseX;
			_cCube.rotationX = this.mouseY - _pStart.y;
		}
		
		private function init3D() {
			view.camera.zoom = 2;
			view.camera.focus = 500;
			
		}

	}
}